<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html><html lang="en">
	<head>
		<!--加载header文件-->
		<?php
		include_once 'public/views/header.php';
		?>
		<link rel="stylesheet" href="public/css/edit.css" />
		<link href="public/css/uploadfile.css" rel="stylesheet">
		<!-- <script src="public/js/jquery.uploadfile.min.js"></script> -->
		<script src="public/js/cus_modal.js"></script>
		<script type="text/javascript" charset="utf-8" src="public/ueditor/ueditor.config.js"></script>
	    <script type="text/javascript" charset="utf-8" src="public/ueditor/ueditor.all.min.js"> </script>
	    <script type="text/javascript" charset="utf-8" src="public/ueditor/lang/zh-cn/zh-cn.js"></script>
		<style>
			.file_input{
				width:70px; height:30px; border:3px solid #6FB3E0; border-radius: 3px; background-color:white; color:#6FB3E0; font-size: 14px; font-weight: bold; line-height: 25px; text-align: center; cursor: pointer;
			}
			.showFileName{font-size: 16px;}
			.file_demo{
				width: 300px;
				/* margin-left: 174px; */
				/* margin-top: -20px; */
				position: relative;
				top: -2px;
				left: 170px;
			}
		</style>
		<script>
		// var ue=UE.getEditor('editor');

		var d;
		var demo;
		var ue=UE.getEditor('editor',{initialFrameWidth:'890'});
		$(function() {
			var type = $("#type").val();
			if(type != "-1"){
				if(type == 1){
					$("#showImage").hide();
					$("#showFile").hide();
					$("#showContent").show();
				}else if(type == 2){
					$("#showImage").show();
					$("#showFile").hide();
					$("#showContent").show();
				}else if(type == 3 || type == 4){
					$("#showImage").hide();
					$("#showFile").show();
					$("#showContent").hide();
				}
			}
			
			ue.addListener("ready", function () { 
		       	UE.getEditor('editor').setContent($('#content').val());
			});
			
			
			//开启验证
			demo = $("#editForm").Validform({
				tiptype: 3
			});
			addDialogButton("保存",save);

			//关闭页面是执行操作
			
		});

		var save = function() { 
			
			if(!demo.check()) {
				$('.Validform_error:first').focus();
				return;
			}
			var id=$('#id').val();
			var title=$('#title').val();
			var type=$('#type').val();
			if(type == "-1"){
				window.parent.topMessagePrompt('error','请选择板块类型');
				return false;
			}

			// var subtitle=$('#subtitle').val();
			var image_url=$('#image_url').val();
			var file_url=$('#file_url').val();
			var file_name=$('#file_name').val();
			if((type == 3 || type == 4) && (file_url == "" || file_url == undefined)){
				window.parent.topMessagePrompt('error','请上传pdf文件');
				return false;
			}

			var content=UE.getEditor('editor').getContent();
			var datas = {id:id,title:title,type:type,image_url:image_url,file_url:file_url,file_name:file_name,content:content}

			d = dialog({
				title: '',
				content: "数据保存中,请稍后...",
			});
			d.showModal();

			$.ajax({
				url: "Admin/IndexLarge/imagetext_save",
				type: "post",
				dataType:"json",
				data: datas,
				success: function(data) {
					if(d != undefined)
						d.close().remove();
					// var ret = $.parseJSON(data);
					var ret = data;
					if(ret.status) {
						var v = $.parseJSON(ret.v);
						if(v == 1) {
							window.parent.refreshTable();
							showDialogModalConfirm(
								'success',
								'保存成功！',
								ret.msg,
								'继续添加',
								'返回列表',
								function(){
									$("form")[0].reset();
								},
								function(){
									hideDialogModal();
								});
						} else {
							window.parent.topMessagePrompt('success',ret.msg);
							window.parent.refreshTable();
							hideDialogModal();
						}

					} else {
						window.parent.topMessagePrompt('error',ret.msg);
					}
				}
			});
		}
	</script>
	</head>
	<body>
		<div class="container">
			<div class="row">
				<input type="hidden" id="modal_title" value="<?=$title ?>" />
				<input type="hidden" id="modal_height" value="450" />
				<form id="editForm">
					<input type="hidden" id="id" name="id" value="<?=estr($detail, 'id') ?>" />
					<table class='edit_table table-condensed'>
						<tr>
							<th>标题</th>
							<td><input  type="text" name="title" id="title" value="<?=estr($detail, 'title') ?>" dataType="*"/></td>
							<th>板块类型</th>
							<td>
							<select name="type" id="type" onchange="show(this)" style="width: 200px;">
								<option value="-1">请选择类型</option>
								<?php foreach ((new ImageType())->getAll() as $k => $v): ?>
									<option value="<?php echo $v[XPHEnum::Value]; ?>" <?php if($v[XPHEnum::Value]==$detail['type']) echo 'selected="selected"'; ?>><?php echo $v[XPHEnum::Caption]; ?></option>
								<?php endforeach; ?>
							</select>

							</td>
						</tr>
					
						<tr id="showImage" style="display: none;">
							<th>展示图片</th>
							<td colspan="3">
							<input type="hidden" name="image_url" value="<?=estr($detail, 'image_url') ?>" id="image_url" >
							<img src="cust/images/upload.png" alt="" style="width:150px; height:70px; cursor: pointer;" onclick="$(this).next().click();" >
							<input type="file" style="display: none;" id="image"  onchange="doImage(this);">
							<?php if(!isset($detail['image_url'])): ?>
								<img src ="" style="width:150px; height:70px; cursor: pointer;display:none;" id="imgurl" >
							<?php else: ?>
								<img src ="<?=estr($detail, 'image_url') ?>" style="width:150px; height:70px; cursor: pointer;" id="imgurl" >
							<?php endif; ?>
							 
							</td>

						</tr>

						<tr id="showFile" style="display: none;">
							<th>Pdf文件</th>
							<td colspan="3">
								<img src="cust/images/upload.png" alt="" style="width:150px; height:70px; cursor: pointer;" onclick="$(this).next().click();" >
								<input type="file" style="display: none;" id="filename" onchange="doMyFile(this);">
								<div class="file_demo">
								<?php if(isset($detail['file_url']) && $detail['file_url'] != ""): ?>
									<div><a href="<?php echo base_url() ?><?=estr($detail, 'file_url') ?>" target="_blank">
											<span class="showFileName"><?=estr($detail, 'filename') ?></span>
											<input type="hidden" name="file_url" id="file_url" value="<?=estr($detail, 'file_url') ?>" datatype="*">
											<input type="hidden" name="filename" id="file_name" value="<?=estr($detail, 'filename') ?>" datatype="*">
										</a>
										<i class="icon-remove-sign del-icon" style="" onclick="delFile(this)"></i>
									</div>
								<?php endif; ?>
								</div>
							</td>

						</tr>
						
						<tr id="showContent" style="display: none;">
							<th>内容</th>
							<td colspan="3">
								<script id="editor" type="text/plain" style="width:600px;height:250px;"></script>
								<textarea name="content" id="content" cols="30" rows="10" style="width:100%; display: none;"><?php echo $detail['content']; ?></textarea>
							</td>
							
						</tr>
						
						
					</table>
				</form>
				<div class="viewContent">
					<!--这是显示具体车辆信息情况的。-->
				</div>
			</div>
		</div>
	</body>

	<script>
		function show(obj){
			var type = $(obj).val();
			if(type == 1){
				$("#showImage").hide();
				$("#showFile").hide();
				$("#showContent").show();
			}else if(type == 2){
				$("#showImage").show();
				$("#showFile").hide();
				$("#showContent").show();
			}else if(type == 3 || type == 4){
				$("#showImage").hide();
				$("#showFile").show();
				$("#showContent").hide();
			}

		}

	function doImage(obj){
		var now_type = 3;
			var fileObj = document.getElementById("image").files; // js 获取文件对象
			if(fileObj.length==0){
				errorAlert('请选择图片');
				return;
			}
			var formFile = new FormData();
			for (var i = 0; i <fileObj.length; i++) {
				formFile.append("myfile", fileObj[i]); //加入文件对象
			}

			var data = formFile;
			setTimeout(function() {
				$.ajax({
					url: 'Admin/CommonUpload/uploadImgFile/' + now_type,
					data: data,
					type: "POST",
					dataType: "json",
					cache: false, //上传文件无需缓存
					processData: false, //用于对data参数进行序列化处理 这里必须false
					contentType: false, //必须
					xhr: xhrOnProgress(function(e) {
						var percent = e.loaded / e.total;
						var now_jindu = (parseFloat(percent) * 100).toFixed(2);
					// console.log(now_jindu);

					}),
					success: function(data) {
						if (data.status) {
							var path=JSON.parse(data.v);
							$("#imgurl").show();
							$("#imgurl").attr("src",path['url']);
							$("#image_url").val(path['url']);	
						} else {
							window.parent.topMessagePrompt('error', data.msg);
						}
					}
				})
			}, 1000);
	}	

	 function doMyFile(obj) {
			
			var now_type = 3;
			var fileObj = document.getElementById("filename").files; // js 获取文件对象
			if(fileObj.length==0){
				errorAlert('请选择文件');
				return;
			}
			var formFile = new FormData();
			for (var i = 0; i <fileObj.length; i++) {
				formFile.append("myfile", fileObj[i]); //加入文件对象
			}

			var data = formFile;
			setTimeout(function() {
				$.ajax({
					url: 'Admin/CommonUpload/uploadPdfFile/' + now_type,
					data: data,
					type: "POST",
					dataType: "json",
					cache: false, //上传文件无需缓存
					processData: false, //用于对data参数进行序列化处理 这里必须false
					contentType: false, //必须
					xhr: xhrOnProgress(function(e) {
						var percent = e.loaded / e.total;
						var now_jindu = (parseFloat(percent) * 100).toFixed(2);
					// console.log(now_jindu);

					}),
					success: function(data) {
						if (data.status) {
							var path=JSON.parse(data.v);
							console.log(path)
							var html = `<div><a href="<?php echo base_url() ?>`+path['url']+`" target="_blank">
										<span class="showFileName">`+path['name']+`</span>
										<input type="hidden" name="file_url" id="file_url" value="`+path['url']+`"  dataType="*">
										<input type="hidden" name="filename" id="file_name" value="`+path['name']+`"  dataType="*">
									</a>
									<i class="icon-remove-sign del-icon" style="" onclick="delFile(this)"></i></di>`;

							$(".file_demo ").html(html);	
							$("#filename").val('')
						
						} else {
							window.parent.topMessagePrompt('error', data.msg);
						}
					}
				})
			}, 1000);
		}


		var xhrOnProgress = function(fun) {
			xhrOnProgress.onprogress = fun; //绑定监听
			//使用闭包实现监听绑
			return function() {
				//通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
				var xhr = $.ajaxSettings.xhr();
				//判断监听函数是否为函数
				if (typeof xhrOnProgress.onprogress !== 'function')
					return xhr;
				//如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
				if (xhrOnProgress.onprogress && xhr.upload) {
					xhr.upload.onprogress = xhrOnProgress.onprogress;
				}
				return xhr;
			}
		}



		//删除文件
		function delFile(obj) {
			var xiabiao=$(obj).parent().index();
			$(obj).parent().eq(xiabiao).remove();
		}

	</script>
	
</html>
